/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {

    $(function () {

        var vip='';
        gettable(vip);

        $("#search").click(function(){
            if(vip===0) {
                vip=1;
            }
            else if(vip===1){
                vip=0;
            }
            gettable(vip);
        });
        $("#showVip").click(function(){

            if(vip===''){
                vip=1;
            }
            gettable(vip);
            if(vip==1){
                vip=0;
                $("#showVip").text("显示普通用户");

            }
            else {
                $("#showVip").text("显示星级用户");
                vip=1;
            }

        });

        //设置vip
        $("#btnisvip").click(function(){
            var vip=$("#hiddenvip").val();
            var pamars={};
            if(vip==0||vip==null){
                pamars.vip=1;
            }
            else
            {
                pamars.vip=0;
            }
            pamars.user_id=$("#hiddenuser_id").val();

            //设置vip
            IO.post("/api/setvip",pamars,function(d){
                if(d.code=="0"){
                    if(d.data.vip==1){
                        $("#btnisvip").css("background-color",'#6ab7ad');
                        $("#btnisvip").html("用户为星级用户");
                        //$("#btnisvip").attr('data-vip','1');
                    }
                    else {
                        $("#btnisvip").css("background-color",'#120');
                        $("#btnisvip").html("标注为星级用户");
                        //$("#btnisvip").attr('data-vip','0');
                    }
                    $("#hiddenvip").val(d.data.vip);
                }
            })

        });
    });

    function gettable(vip){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '用户ID',
                dataIndex: 'user_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].user_id + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '用户昵称',
                dataIndex: 'nickname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].nickname + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '注册时间',
                dataIndex: 'createdDate',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].createdDate + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '手机号',
                dataIndex: 'nickname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].mobile + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '怀孕状态',
                dataIndex: 'nickname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].pregnancy_status==0){
                        return "<span>" + "没怀孕" + "</span>";
                    }
                    else {
                        return "<span>" + "怀孕中" + "</span>";
                    }

                }
            },
            {
                title: '怀孕日期',
                dataIndex: 'nickname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].pregnancy_status==0){
                        return "<span>无</span>";
                    }else{
                        return "<span>" + data[datarow].pregnancy_date + "</span>";
                    }
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' is_vip="+data[datarow].vip+"  act_id="+data[datarow].user_id+" class='details'><span style='color: #6ab7ad;'>" + "详情" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/userslist", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(vip), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param(vip) {
            var mobile= $("#txtPhone").val();
            var param="mobile="+mobile+"&vip="+vip;
            return param;
        }
        dataInit();
    }

    $(document).on("click",".details",function(){
        var act_id=$(this).attr("act_id");
        //var is_vip=$(this).attr("is_vip");
        //$("#btnisvip").attr("data-act",act_id);
        //is_vip=0为普通用户
        //is_vip=1;

        //if(is_vip==1){
        //    $("#btnisvip").css("background-color",'#6ab7ad');
        //    $("#btnisvip").html("用户为星级用户");
        //    $("#btnisvip").attr('data-vip','1');
        //
        //}
        //else {
        //    $("#btnisvip").css("background-color",'#120');
        //    $("#btnisvip").html("标注为星级用户");
        //    $("#btnisvip").attr('data-vip','0');
        //}

        if(act_id){
            //
            IO.get("/api/users",{id:act_id},function(d){

                var data= d.data;
                if(data.vip==null||data.vip==0){
                    $("#btnisvip").css("background-color",'#120');
                    $("#btnisvip").html("标注为星级用户");
                    //$("#btnisvip").attr('data-vip','0');
                }
                else {
                    $("#btnisvip").css("background-color",'#6ab7ad');
                    $("#btnisvip").html("用户为星级用户");
                }
                //console.log(data);
                $('#myModal input').attr('readonly','readonly');
                //用户ID
                $("#hiddenuser_id").val(data.user_id);
                //vip
                $("#hiddenvip").val(data.vip);
                //头像
                $("#headimage").attr('src',data.headimage);
                //姓名
                $("#user_name").val(data.nickname);
                //手机号
                $("#user_phone").val(data.mobile);
                //育币数
                $("#integral").val(data.integral);
                //怀孕状态
                if(data.pregnancy_status==1){
                    $("#user_state").val("怀孕");
                }
                else {
                    $("#user_state").val("未怀孕");
                }
                //怀孕时间
                $("#user_date").val(data.pregnancy_date);
                //咨询次数
                $("#user_count").val(data.consult_count);

                //家政服务次数
                $("#diary_count").val(data.diary_count);

                $('#myModal').modal();
            });
            //先查数据绑定到页面上,然后手动调用modal
        }
    });
});
